#pragma once
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <opencv/highgui.h>

class SparseFilter
{
public:
	static void filter(IplImage* srcImg, IplImage* dstImg,int filterWidth=3,int thesh=5,int iteration=3){
		for(int h=0;h<srcImg->height;h++){
			for(int w=0;w<srcImg->widthStep;w++){
				if(h<filterWidth || w<filterWidth ||
					h+filterWidth>srcImg->height || w+filterWidth>srcImg->widthStep){
						*((uchar*)(dstImg->imageData+h*srcImg->widthStep+w)) = 0;
						continue;
				}

				// 
				int pixels = 0;
				for(int i=h-filterWidth/2;i<=filterWidth/2+h;i++){
					for (int j=w-filterWidth/2;j<=filterWidth/2+w;j++){
						int pixelValue =  *((uchar*)(srcImg->imageData + i*srcImg->widthStep + j));
						if( pixelValue > 200) pixels++;
					}
				}
				// write the filter result to the dst image
				*((uchar*)(dstImg->imageData+h*srcImg->widthStep+w)) = (pixels<thesh)?0:255;
			}
		}

		
	}


};

